<?php
/**
 * Created by PhpStorm.
 * User: Liulibao
 * Date: 2019/10/11
 * Time: 20:37
 */

namespace app\common\validate;


use app\common\model\Member;
use think\Log;
use think\Validate;

class Addselleraccount extends Validate
{
    protected $rule = [
        ['member_name', 'require', '用户账号不能为空'],
        ['member_password', 'require|length:6,20', '密码不能为空|密码长度必须为6-20之间'],
        ['member_passwords', 'length:6,20', '密码长度必须为6-20之间'],  #修改密码时使用
        ['member_truename', 'require|checkTruename', '真实姓名不能为空|真实姓名错误'],
        ['member_idcard', 'require|checkIdcard|checkIdcardUnique', '身份证号码不能为空|身份证错误|身份证号码已存在'],
        ['member_idcards', 'require|checkIdcard', '身份证号码不能为空|身份证错误'],
        ['member_mobile', 'require|length:11,11', '手机号不能为空|手机格式错误'],
    ];

    protected $scene = [
        'members' => ['member_name', 'member_password','member_truename', 'member_idcard', 'member_mobile'],
        'update_member' => ['member_passwords','member_truename', 'member_idcards', 'member_mobile']
    ];

    protected function checkTruename($value,$rule,$data)
    {
        return preg_match('/^[\x{4e00}-\x{9fa5}]{2,4}$/u',$value)? true: false;
    }
    protected function checkIdcard($value,$rule,$data)
    {
        return preg_match('/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/',$value)?true:false;
    }
    protected function checkIdcardUnique($value,$rule,$data)
    {
        $result = (new Member())->getMemberInfo(array('member_idcard' => $value), 'member_id');
       return  !empty($result) ? false : true;
    }
}